package com.zhende.hr.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

/**
 * 考勤数据库（192.168.0.137,1322，kglog）
 * 
* @Package com.zhende.out.config 
* @Description: TODO(用一句话描述该文件做什么) 
* @author fmf   
* @date 2020年9月25日 上午9:02:22 
* @version V1.0
 */
@Configuration
@MapperScan(basePackages = {
		"com.zhende.hr.mapper.kqlog" }, sqlSessionFactoryRef = "db5SqlSessionFactory", sqlSessionTemplateRef = "db5SqlSessionTemplate")
public class DbKqLogConfig {
	private Logger logger = LoggerFactory.getLogger(DbKqLogConfig.class);

	@Value("${spring.datasource.druid.five.url}")
	private String dbUrl;
	// oracle 账号
	@Value("${spring.datasource.druid.five.username}")
	private String username;
	// oracle 密码
	@Value("${spring.datasource.druid.five.password}")
	private String password;
	// oracle 驱动名
	@Value("${spring.datasource.druid.five.driverClassName}")
	private String driverClassName;
	// 初始化大小
	@Value("${spring.datasource.druid.five.initialSize}")
	private int initialSize;
	// 最小连接
	@Value("${spring.datasource.druid.five.minIdle}")
	private int minIdle;
	// 最大连接
	@Value("${spring.datasource.druid.five.maxActive}")
	private int maxActive;
	// 配置获取连接等待超时的时间
	@Value("${spring.datasource.druid.five.maxWait}")
	private int maxWait;
	// 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
	@Value("${spring.datasource.druid.five.timeBetweenEvictionRunsMillis}")
	private int timeBetweenEvictionRunsMillis;
	// 配置一个连接在池中最小生存的时间，单位是毫秒
	@Value("${spring.datasource.druid.five.minEvictableIdleTimeMillis}")
	private int minEvictableIdleTimeMillis;
	// 检验连接sql
	@Value("${spring.datasource.druid.five.validationQuery}")
	private String validationQuery;
	/**
	 * 起了一个 异步Evict的TimerTask定时线程进行控制 ( 可通过设置参数 timeBetweenEvictionRunsMillis>0),
	 * 定时对线程池中的链接进行validateObject校验， 对无效的链接进行关闭后，会调用ensureMinIdle，
	 * 适当建立链接保证最小的minIdle连接数
	 */
	@Value("${spring.datasource.druid.five.testWhileIdle}")
	private boolean testWhileIdle;
	// 进行borrowObject进行处理时，对拿到的connection进行validateObject校验
	@Value("${spring.datasource.druid.five.testOnBorrow}")
	private boolean testOnBorrow;
	// 进行returnObject对返回的connection进行validateObject校验，个人觉得对数据库连接池的管理意义不大
	@Value("${spring.datasource.druid.five.testOnReturn}")
	private boolean testOnReturn;
	// 打开PSCache，并且指定每个连接上PSCache的大小
	@Value("${spring.datasource.druid.five.poolPreparedStatements}")
	private boolean poolPreparedStatements;
	// 配置监控统计拦截的filters，去掉后监控界面sql无法统计，'wall'用于防火墙
	@Value("${spring.datasource.druid.five.filters}")
	private String filters;
	// 连接超时关闭
	@Value("${spring.datasource.druid.five.removeAbandoned}")
	private boolean removeAbandoned;
	// 设置连接超时关闭实际那
	@Value("${spring.datasource.druid.five.removeAbandonedTimeout}")
	private int removeAbandonedTimeout;

	@Bean()
	@ConfigurationProperties(prefix = "spring.datasource.druid.five")
	public DataSource db5DataSource() {

      return DruidDataSourceBuilder.create().build();
	}

	@Bean
	public SqlSessionFactory db5SqlSessionFactory(@Qualifier("db5DataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/kqlog/*.xml"));
		return bean.getObject();
	}

	@Bean
	public DataSourceTransactionManager db5TransactionManager(@Qualifier("db5DataSource") DataSource dataSource) {
		return new DataSourceTransactionManager(dataSource);
	}

	@Bean
	public SqlSessionTemplate db5SqlSessionTemplate(
			@Qualifier("db5SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
		return new SqlSessionTemplate(sqlSessionFactory);
	}

}
